package com.masterclient.app.businessmodule.homemodule.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.adapter.MeetReportAdapter;
import com.masterclient.app.businessmodule.homemodule.contract.MeetReportContract;
import com.masterclient.app.businessmodule.homemodule.contract.MeetReportPresenter;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.income.model.MeetReportDataBean;
import com.masterclient.app.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/4 13:17
 * 功能说明：
 */
@Route(path = AroutePath.MEET_REPORT_FRAGMENT)
public class MeetReportFragment extends BaseFragment<MeetReportPresenter> implements MeetReportContract.View {
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    List<MeetReportDataBean.NewsItem> list;
    private boolean hasMore = true;
    private int page = 1;
    private String headUrl;

    @Autowired(name = "page_name")
    String pageName;

    private MeetReportAdapter recycleAdapter;

    public static MeetReportFragment getInstance(String pageName) {
        MeetReportFragment fragment = new MeetReportFragment();
        Bundle bundle = new Bundle();
        bundle.putString("page_name", pageName);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_meet_report, null);
    }

    @Override
    protected void initOnlyOnce() {

        ARouter.getInstance().inject(this);
        if (pageName.equals("news")) {
            tvTitleName.setText("大会报道");
        } else if (pageName.equals("historys")) {
            tvTitleName.setText("往届风采");
        } else if (pageName.equals("views")) {
            tvTitleName.setText("风景名胜");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleAdapter = new MeetReportAdapter();
        recycleAdapter.setOnItemClickListener((adapter, view, position) -> {
            MeetReportDataBean.NewsItem item = list.get(position);
            CommonUtil.goAnyWhere(mContext, item.url, "", "", "", "", "");
        });
        recyclerView.setAdapter(recycleAdapter);
        recycleAdapter.bindToRecyclerView(recyclerView);

        page = 1;
        mPresenter.getNews(String.valueOf(page), PAGE_SIZE, pageName);
        recycleAdapter.setOnLoadMoreListener(() -> {
            if (hasMore) {
                page++;
                mPresenter.getNews(String.valueOf(page), PAGE_SIZE, pageName);
            }
        }, recyclerView);

        swipeLayout.setOnRefreshListener(() -> {
            if (list != null) {
                list.clear();
                recycleAdapter.notifyDataSetChanged();
            }
            recycleAdapter.setEnableLoadMore(true);
            page = 1;
            mPresenter.getNews(String.valueOf(page), PAGE_SIZE, pageName);

        });
        super.initOnlyOnce();
    }

    @OnClick({R.id.rl_back, R.id.view_head})
    protected void onClick(View view) {
        if (view.getId() == R.id.rl_back) {
            getActivity().finish();
        } else if (view.getId() == R.id.view_head) {
            if (!TextUtils.isEmpty(headUrl)) {
                CommonUtil.goAnyWhere(mContext, headUrl, "", "", "", "", "");
            }
        }
    }

    @Override
    public void getNewsInfoSuccess(MeetReportDataBean dataBean) {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        this.page = Integer.parseInt(dataBean.page);
        if (list == null) {
            list = new ArrayList<>();
        }

        List<MeetReportDataBean.NewsItem> dataList = null;
        if (pageName.equals("news")) {
            dataList = dataBean.news;
        } else if (pageName.equals("historys")) {
            dataList = dataBean.historys;
        } else if (pageName.equals("views")) {
            dataList = dataBean.views;
        }
        if (page == 1) {
            //头部信息
            if (dataList != null && dataList.size() > 0) {
                headUrl = dataList.get(0).url;
                tvTitle.setText(dataList.get(0).name);
                tvTime.setText(dataList.get(0).createTime);
                GlideUtil.displayImage(mContext, dataList.get(0).image, ivImg, R.mipmap.defaults);
            }

            list.clear();
            page++;
            mPresenter.getNews(String.valueOf(page), PAGE_SIZE, pageName);
        }
        list.addAll(dataList);
        recycleAdapter.setNewData(list);
        recycleAdapter.setEnableLoadMore(true);
        hasMore = true;
        if (page > 1 && (dataList == null || dataList.size() == 0)) {
            hasMore = false;
            recycleAdapter.setEnableLoadMore(false);
        }
        recycleAdapter.notifyDataSetChanged();
    }

    @Override
    public void getNewsInfoFailed(String msg) {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }
}
